
package com.fcab.tourfcab;

import java.io.FileOutputStream;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.view.View.OnClickListener;

import com.metaio.sdk.MetaioDebug;
import com.metaio.sdk.SensorsComponentAndroid;
import com.metaio.sdk.jni.IGeometry;
import com.metaio.sdk.jni.IMetaioSDKCallback;
import com.metaio.sdk.jni.IRadar;
import com.metaio.sdk.jni.LLACoordinate;
import com.metaio.tools.io.AssetsManager;

import com.fcab.domain.PointGps;
import com.fcab.marker.MetaioSDKViewActivity;
import com.fcab.tourfcab.R;

public class ActivityPointGPS extends MetaioSDKViewActivity  implements SensorsComponentAndroid.Callback 
{
	
	//Geometries
	private IGeometry mGeometryPoint1,mGeometryPoint2,mGeometryPoint3,mGeometryPoint4,mGeometryPoint5,mGeometryPoint6,mGeometryPoint7,mGeometryPoint8,
					  mGeometryPoint9,mGeometryPoint10,mGeometryPoint11,mGeometryPoint12,mGeometryPoint13,mGeometryPoint14,mGeometryPoint15;
	
	private IRadar mRadar;
	private PointGps pGps;
	
	//Dialog
	final Context context = this;

	//Offset from current location
	private static final double OFFSET = 		0.00002;
	
	@Override
	public void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		boolean result = metaioSDK.setTrackingConfiguration("GPS");
		pGps = new PointGps();
		
		MetaioDebug.log("Tracking data loaded: " + result);  
	}
	
	@Override
	protected void onPause() 
	{
		super.onPause();
		if (mSensors != null)
		{
			mSensors.registerCallback(null);
		}
	}

	@Override
	protected void onResume() 
	{
		super.onResume();
		if (mSensors != null)
		{
			mSensors.registerCallback(this);
		}
		
	}

	@Override
	public void onLocationSensorChanged(LLACoordinate location)
	{
		updateGeometriesLocation(location);
	}

	public void onButtonClick(View v)
	{
		finish();
	}

	@Override
	protected int getGUILayout() 
	{
		return R.layout.activitypointgps;
	}

	@Override
	protected IMetaioSDKCallback getMetaioSDKCallbackHandler() 
	{
		return null;
	}

	@Override
	protected void loadContent() 
	{
		try
		{ 
			String filepath = AssetsManager.getAssetPath("Assets_radar/POI_bg.png");
			if (filepath != null) 
			{
				mGeometryPoint1 = metaioSDK.loadImageBillboard(createBillboardTexture("Point 1"));
				mGeometryPoint1.setName("geometry1");
				
				mGeometryPoint4 = metaioSDK.loadImageBillboard(createBillboardTexture("Point 4"));
				mGeometryPoint4.setName("geometry4");
				
				mGeometryPoint6 = metaioSDK.loadImageBillboard(createBillboardTexture("Point 6"));
				mGeometryPoint6.setName("geometry6");
				
				mGeometryPoint7 = metaioSDK.loadImageBillboard(createBillboardTexture("Point 7"));
				mGeometryPoint7.setName("geometry7");
				
				mGeometryPoint15 = metaioSDK.loadImageBillboard(createBillboardTexture("Point 15"));
				mGeometryPoint15.setName("geometry15");
			}
			
			updateGeometriesLocation(mSensors.getLocation());
			
			// create radar
			mRadar = metaioSDK.createRadar();
			mRadar.setBackgroundTexture(AssetsManager.getAssetPath("Assets_radar/radar.png"));
			mRadar.setObjectsDefaultTexture(AssetsManager.getAssetPath("Assets_radar/yellow.png"));
			mRadar.setRelativeToScreen(IGeometry.ANCHOR_TL);
							
			// add geometries to the radar
			mRadar.add(mGeometryPoint1);
			mRadar.add(mGeometryPoint4);
			mRadar.add(mGeometryPoint6);
			mRadar.add(mGeometryPoint7);
			mRadar.add(mGeometryPoint15);
		}
		
		catch (Exception e)
		{
			MetaioDebug.log("Error method loadContent");	
		}
		
	}
	
	private String createBillboardTexture(String billBoardTitle)
    {
           try
           {
                  final String texturepath = getCacheDir() + "/" + billBoardTitle + ".png";
                  Paint mPaint = new Paint();

                  // Load background image (256x128), and make a mutable copy
                  Bitmap billboard = null;
                  
                  //reading billboard background
                  String filepath = AssetsManager.getAssetPath("Assets_radar/POI_bg.png");
                  Bitmap mBackgroundImage = BitmapFactory.decodeFile(filepath);
                  
                  billboard = mBackgroundImage.copy(Bitmap.Config.ARGB_8888, true);


                  Canvas c = new Canvas(billboard);

                  mPaint.setColor(Color.WHITE);
                  mPaint.setTextSize(24);
                  mPaint.setTypeface(Typeface.DEFAULT);

                  float y = 40;
                  float x = 30;

                  // Draw POI name
                  if (billBoardTitle.length() > 0)
                  {
                        String n = billBoardTitle.trim();

                        final int maxWidth = 160;

                        int i = mPaint.breakText(n, true, maxWidth, null);
                        c.drawText(n.substring(0, i), x, y, mPaint);

                        // Draw second line if valid
                        if (i < n.length())
                        {
                               n = n.substring(i);
                               y += 20;
                               i = mPaint.breakText(n, true, maxWidth, null);

                               if (i < n.length())
                               {
                                      i = mPaint.breakText(n, true, maxWidth - 20, null);
                                      c.drawText(n.substring(0, i) + "...", x, y, mPaint);
                               } else
                               {
                                      c.drawText(n.substring(0, i), x, y, mPaint);
                               }
                        }

                  }


                  // writing file
                  try
                  {
                	  FileOutputStream out = new FileOutputStream(texturepath);
                      billboard.compress(Bitmap.CompressFormat.PNG, 90, out);
                      MetaioDebug.log("Texture file is saved to "+texturepath);
                      return texturepath;
                  } catch (Exception e) {
                      MetaioDebug.log("Failed to save texture file");
                	  e.printStackTrace();
                   }
                 
                  billboard.recycle();
                  billboard = null;

           } catch (Exception e)
           {
                  MetaioDebug.log("Error creating billboard texture: " + e.getMessage());
                  MetaioDebug.printStackTrace(Log.DEBUG, e);
                  return null;
           }
           return null;
    }
	
	private void updateGeometriesLocation(LLACoordinate location)
	{
		if (mGeometryPoint1 != null)
		{
			location.setLatitude(pGps.getPoint(1).latitude -OFFSET);
			location.setLongitude(pGps.getPoint(1).longitude -OFFSET);
			
			MetaioDebug.log("geometrySouth.setTranslationLLA: "+location);
			mGeometryPoint1.setTranslationLLA(location);
			
			location.setLatitude(pGps.getPoint(1).latitude +OFFSET);
			location.setLongitude(pGps.getPoint(1).longitude +OFFSET);
		}
		
		if (mGeometryPoint4 != null)
		{
			location.setLatitude(pGps.getPoint(4).latitude -OFFSET);
			location.setLongitude(pGps.getPoint(4).longitude -OFFSET);
			
			MetaioDebug.log("geometrySouth.setTranslationLLA: "+location);
			mGeometryPoint4.setTranslationLLA(location);
			
			location.setLatitude(pGps.getPoint(4).latitude +OFFSET);
			location.setLongitude(pGps.getPoint(4).longitude +OFFSET);
		}
				
		
		if (mGeometryPoint6 != null)
		{
			location.setLatitude(pGps.getPoint(6).latitude -OFFSET);
			location.setLongitude(pGps.getPoint(6).longitude -OFFSET);
			
			MetaioDebug.log("geometrySouth.setTranslationLLA: "+location);
			mGeometryPoint6.setTranslationLLA(location);
			
			location.setLatitude(pGps.getPoint(6).latitude +OFFSET);
			location.setLongitude(pGps.getPoint(6).longitude +OFFSET);
		}
		
		if (mGeometryPoint7 != null)
		{
			location.setLatitude(pGps.getPoint(7).latitude -OFFSET);
			location.setLongitude(pGps.getPoint(7).longitude -OFFSET);
			
			MetaioDebug.log("geometrySouth.setTranslationLLA: "+location);
			mGeometryPoint7.setTranslationLLA(location);
			
			location.setLatitude(pGps.getPoint(7).latitude +OFFSET);
			location.setLongitude(pGps.getPoint(7).longitude +OFFSET);
		}
		
		if (mGeometryPoint15 != null)
		{
			location.setLatitude(pGps.getPoint(15).latitude -OFFSET);
			location.setLongitude(pGps.getPoint(15).longitude -OFFSET);
			
			MetaioDebug.log("geometrySouth.setTranslationLLA: "+location);
			mGeometryPoint15.setTranslationLLA(location);
			
			location.setLatitude(pGps.getPoint(15).latitude +OFFSET);
			location.setLongitude(pGps.getPoint(15).longitude +OFFSET);
		}
	}
	
	@Override
	protected void onGeometryTouched(final IGeometry geometry) 
	{
		MetaioDebug.log("Geometry selected: "+geometry);
		
		mSurfaceView.queueEvent(new Runnable()
		{

			@Override
			public void run() 
			{
				mRadar.setObjectsDefaultTexture(AssetsManager.getAssetPath("Assets_radar/yellow.png"));
				mRadar.setObjectTexture(geometry, AssetsManager.getAssetPath("Assets_radar/red.png"));
				
			}
		});
		
		final Dialog dialog = new Dialog(context,R.style.Theme_Transparent);
 		dialog.setContentView(R.layout.dialog);
 		
 		TextView text = (TextView) dialog.findViewById(R.id.text);
 		ImageView image = (ImageView) dialog.findViewById(R.id.image);
 		
 		Button dialogBtnClose = (Button) dialog.findViewById(R.id.dialogButtonClose);
		Button dialogBtnVideo = (Button) dialog.findViewById(R.id.dialogButtonVideo);
		Button dialogBtnPanorama = (Button) dialog.findViewById(R.id.dialogButtonVista360);
		Button dialogBtn3D = (Button) dialog.findViewById(R.id.dialogButtonVista3D);
		
		//Dialog Geometry 1
	     if (geometry.getName().equals("geometry1")) {
	 		dialog.setTitle(R.string.titulo_uno);	
	 		text.setText(R.string.historia_uno);
	 		image.setImageResource(R.drawable.primerpunto);
	 		// if button is clicked, close the custom dialog
	 		
	 		dialogBtnClose.setOnClickListener(new OnClickListener() {
	 			@Override
	 			public void onClick(View v) {
	 				dialog.dismiss();
	 			}
	 		});
	 		
	 		dialogBtnVideo.setVisibility(View.INVISIBLE);
	 		dialogBtnPanorama.setVisibility(View.INVISIBLE);
	 		dialogBtn3D.setVisibility(View.VISIBLE);
	 		dialog.show();
	     }
 		
 		 //Dialog Geometry 4
	     if (geometry.getName().equals("geometry4")) {
	 		dialog.setTitle(R.string.titulo_cuatro);	
	 		text.setText(R.string.historia_cuatro);
	 		image.setImageResource(R.drawable.cuartopunto);
	 		// if button is clicked, close the custom dialog
	 		
	 		dialogBtnClose.setOnClickListener(new OnClickListener() {
	 			@Override
	 			public void onClick(View v) {
	 				dialog.dismiss();
	 			}
	 		});
	 		
	 		dialogBtnVideo.setVisibility(View.INVISIBLE);
	 		dialogBtnPanorama.setVisibility(View.INVISIBLE);
	 		dialogBtn3D.setVisibility(View.INVISIBLE);
	 		
	 		dialog.show();
	     }
	     
	   //Dialog Geometry 6
	     if (geometry.getName().equals("geometry6")) {
		 		dialog.setTitle(R.string.titulo_seis);	
		 		text.setText(R.string.historia_seis);
		 		image.setImageResource(R.drawable.sextopunto);
		 		// if button is clicked, close the custom dialog
		 		dialogBtnClose.setOnClickListener(new OnClickListener() {
		 			@Override
		 			public void onClick(View v) {
		 				dialog.dismiss();
		 			}
		 		});
		 		
		 		dialogBtnVideo.setVisibility(View.VISIBLE);
		 		dialogBtnPanorama.setVisibility(View.INVISIBLE);
		 		dialogBtn3D.setVisibility(View.INVISIBLE);
		 		dialog.show();
		 }
	     
	     
		   //Dialog Geometry 7
		     if (geometry.getName().equals("geometry7")) {
			 		dialog.setTitle(R.string.titulo_siete);	
			 		text.setText(R.string.historia_siete);
			 		image.setImageResource(R.drawable.septimopunto);
			 		// if button is clicked, close the custom dialog
			 		dialogBtnClose.setOnClickListener(new OnClickListener() {
			 			@Override
			 			public void onClick(View v) {
			 				dialog.dismiss();
			 			}
			 		});
			 		
			 		dialogBtnVideo.setVisibility(View.INVISIBLE);
			 		dialogBtnPanorama.setVisibility(View.VISIBLE);
			 		dialogBtn3D.setVisibility(View.INVISIBLE);
			 		dialog.show();
			 }
	     
	   //Dialog Geometry 15
	     if (geometry.getName().equals("geometry15")) {
		 		dialog.setTitle(R.string.titulo_quince);	
		 		text.setText(R.string.historia_quince);
		 		image.setImageResource(R.drawable.quinceavopunto);
		 		// if button is clicked, close the custom dialog
		 		dialogBtnClose.setOnClickListener(new OnClickListener() {
		 			@Override
		 			public void onClick(View v) {
		 				dialog.dismiss();
		 			}
		 		});
		 		
		 		dialogBtnVideo.setVisibility(View.INVISIBLE);
		 		dialogBtnPanorama.setVisibility(View.INVISIBLE);
		 		dialogBtn3D.setVisibility(View.INVISIBLE);
		 		dialog.show();
		     }
	}

	@Override
	public void onGravitySensorChanged(float[] gravity) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onHeadingSensorChanged(float[] orientation) {
		// TODO Auto-generated method stub
		
	}

}
